﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class ItemMemberList : PageBase
    {
        

        public string ItemID
        {
            get
            {
                var temp = GetQueryIntValue("ItemID");
                return string.IsNullOrEmpty(temp) ? "" : temp;
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "GrassrootsGroupAdd";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            btnNew.OnClientClick = Window1.GetShowReference("ItemMemberEdit.aspx?ItemID=" + ItemID);
            ControlBusiness.ControlBindDataDictionary(
                ddlCategory, SysDataDictionary.GrassrootsDutyCategory, true);
            // 每页记录数
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();
            Grid1.PageSize = ConfigHelper.PageSize;
  
        }

        private void LoadData()
        {
            if (string.IsNullOrEmpty(ItemID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            var ds = PartyGrassrootsGroupItemMember.GetGridData(Grid1.PageIndex, Grid1.PageSize, ItemID,this.ddlCategory.SelectedValue ,txtTitle.Text);
            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("GrassrootsGroupAdd", Grid1, "editField");
            CheckPowerWithLinkButtonField("GrassrootsGroupAdd", Grid1, "deleteField");
        }

        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            //DataRowView row = e.DataItem as DataRowView;
            //var btnHandler = Grid1.FindColumn("btnHandler") as LinkButtonField;

            //if (btnHandler != null)
            //{
            //    this.Window1.Title = Convert.ToString(row["Title"]);
            //    var url = Convert.ToString(row["TaskUrl"]);
            //    if (url.Contains("?"))
            //    {
            //        url += "&TaskID=" + row["ID"];
            //    }
            //    else
            //    {
            //        url += "?TaskID=" + row["ID"];
            //    }
            //    btnHandler.OnClientClick = this.Window1.GetShowReference(url);
            //}
        }
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string memberId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("GrassrootsGroupAdd"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }
                var del = DB.PartyGrassrootsGroupItemMembers.FirstOrDefault(t => t.ID == memberId);
                DB.PartyGrassrootsGroupItemMembers.Remove(del);
               
                DB.SaveChanges();
                LoadData();
            }
        }
        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("GrassrootsGroupAdd"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyGrassrootsGroupItemMembers.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyGrassrootsGroupItemMembers.Remove(u));
            DB.SaveChanges();
 

            // 重新绑定表格
            LoadData();
        }




        #endregion

        protected void Window1_Close(object sender, EventArgs e)
        {
            LoadData();
        }


        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }
    }
}
